package com.caul.demo.hadoop.mapreduce.rank;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * Created by sdliang on 2018/4/11.
 */
public class RankPartitioner extends Partitioner<RankKey, NullWritable> {

  /**
   * hashCode()可能操作Integer.MAX_VALUE,导致% numPartitions为负数
   * @param rankKey
   * @param nullWritable
   * @param numPartitions
   * @return
   */
  @Override
  public int getPartition(RankKey rankKey, NullWritable nullWritable, int numPartitions) {
    return (rankKey.getUid().hashCode() & Integer.MAX_VALUE) % numPartitions;
  }
}
